/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package id3.models;

import java.util.ArrayList;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author tungck
 */
public class TreeNode {

    private Attribute attrNode;
    private ArrayList<TreeNode> mChilds;
    private ArrayList<String> mValues;
    
    /**
     * luu lai root ccua cay
     */
    public static TreeNode instance=null; 

    public TreeNode(Attribute attrNode) {
        this.attrNode = attrNode;
        mChilds = new ArrayList<>();
        mValues = new ArrayList<>();
    }

    public void addChilds(TreeNode node) {
        mChilds.add(node);
    }

    public void addValues(String value) {
        mValues.add(value);
    }

    public ArrayList<String> getmValues() {
        return mValues;
    }

    public Attribute getAttrNode() {
        return attrNode;
    }

    public ArrayList<TreeNode> getmChilds() {
        return mChilds;
    }

    public DefaultTreeModel convertToTree() {
        DefaultTreeModel model = new DefaultTreeModel(getTreeNode("","",this));
        return model;
    }

    private DefaultMutableTreeNode getTreeNode(String parentName,String parentValue,TreeNode root) {
        DefaultMutableTreeNode result = new DefaultMutableTreeNode();
        if (root.attrNode.getLabel() != null) {
            result.setUserObject("["+parentValue+"] "+root.attrNode.getLabel());
        } else {
            StringBuilder s=new StringBuilder();
            //s.append("[").append(parentName).append("=").append(parentValue).append("] ").append(root.getAttrNode().getName());
            s.append("[").append(parentValue).append("] ").append(root.getAttrNode().getName());
            result.setUserObject(s.toString());
            int i=0;
            for (TreeNode chd : root.mChilds) {
                result.add(getTreeNode(root.attrNode.getName(),root.mValues.get(i),chd));
                i++;
            }
        }
        return result;
    }
}
